'''
Created on 31/03/2010

@author: miguel
'''
import pygame
from GameBoard import GameBoard
from pygame.sprite import Sprite
from vec2d import vec2d

class Box(Sprite):
    (CLOSED, HALF_OPEN, OPEN) = range(3)
    (SUPPORT, SURPRISE1, SURPRISE2) = range(3)
    IMAGE_HALF_OPEN = './data/box_half_open.png'
    IMAGE_CLOSED = './data/box.png'
    IMAGE_OPEN_SUPPORT = './data/box_open_support.png'
    IMAGE_OPEN_SURPRISE1 = './data/box_open_surprise1.png'
    IMAGE_OPEN_SURPRISE2 = './data/box_open_surprise2.png'

    def __init__(self, screen, game, field, position, object_type, owner):
        """initial constructor
        
        Keyword arguments:
        screen -- the screen where the box will be drawn
        game -- the main object of the game which contains everything needed
        field -- square where the box will be drawn
        position -- position where the box will be drawn
        object_type -- type of the object: SUPPORT, SURPRISE1 or SURPRISE2
        owner -- owner of the object; the player or the computer
        
        """
        
        Sprite.__init__(self)
        
        self.screen = screen
        self.field = field
        self.position = vec2d(position)
        self.object = object_type
        self.owner = owner
        self.game = game
        self.status = self.CLOSED

        self.image_box_closed = pygame.image.load(self.IMAGE_CLOSED)
        self.image_box_half_open = pygame.image.load(self.IMAGE_HALF_OPEN)
        if self.object == self.SUPPORT:
            self.image_open = pygame.image.load(self.IMAGE_OPEN_SUPPORT)
        elif self.object == self.SURPRISE1:
            self.image_open = pygame.image.load(self.IMAGE_OPEN_SURPRISE1)
        elif self.object == self.SURPRISE2:
            self.image_open = pygame.image.load(self.IMAGE_OPEN_SURPRISE2)
        self.image = self.image_box_closed

        self.rect = self.image.get_rect().move(self.position)
        if self.owner == GameBoard.USER_BOX:
            self.game.game_board.take_position(self.position, GameBoard.USER_BOX)
        else:
            self.game.game_board.take_position(self.position, GameBoard.COM_BOX)

        self.image = self.image_box_closed

    def hit_box(self, position_list):
        '''function to hit the box
        
        Keyword arguments:
        position_list -- list of positions
         
        '''
        
        if self.status != self.OPEN:
            pass
            if self.position == position_list[0]:
                self.status = self.OPEN
                self.image = self.image_open
                if self.object == self.SUPPORT:
                    if self.owner == GameBoard.USER_BOX:
                        self.game.player.support_discovered()
                    elif self.owner == GameBoard.COM_BOX:
                        self.game.computer.support_discovered()
                elif self.object == self.SURPRISE1 or self.object == self.SURPRISE2:
                    if self.owner == GameBoard.USER_BOX:
                        self.game.player.surprise_discovered()
                    elif self.owner == GameBoard.COM_BOX:
                        self.game.computer.surprise_discovered()
            elif self.position == position_list[1] or self.position == position_list[2] or self.position == position_list[3] or self.position == position_list[4]:
                if self.status == self.CLOSED:
                    self.status = self.HALF_OPEN
                    self.image = self.image_box_half_open
                elif self.status == self.HALF_OPEN:
                    self.status = self.OPEN
                    self.image = self.image_open
                    if self.object == self.SUPPORT:
                        if self.owner == GameBoard.USER_BOX:
                            self.game.player.support_discovered()
                        elif self.owner == GameBoard.COM_BOX:
                            self.game.computer.support_discovered()
                    elif self.object == self.SURPRISE1 or self.object == self.SURPRISE2:
                        if self.owner == GameBoard.USER_BOX:
                            self.game.player.surprise_discovered()
                        elif self.owner == GameBoard.COM_BOX:
                            self.game.computer.surprise_discovered()

    def update(self):
        pass


    def draw(self):
        self.screen.blit(self.image, self.rect)
